/**
 * Created by Hehe on 2017/8/3.
 */

var DJangoHelp = {

    csrfSafeMethod: function (method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    },

    sameOrigin: function (url) {
        // test that a given url is a same-origin URL
        // url could be relative or scheme relative or absolute
        var host = document.location.host; // host + port
        var protocol = document.location.protocol;
        var sr_origin = '//' + host;
        var origin = protocol + sr_origin;
        // Allow absolute or scheme relative URLs to same origin
        return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
            (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
            // or any other URL that isn't scheme relative or absolute i.e relative.
            !(/^(\/\/|http:|https:).*/.test(url));
    },

    getCookie: function (name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    },

    ajaxSetupCSRFToken: function () {
        $.ajaxSetup({
            beforeSend: function (xhr, settings) {
                if (!JqGridHelp.csrfSafeMethod(settings.type) && JqGridHelp.sameOrigin(settings.url)) {
                    var csrftoken = DJangoHelp.getCookie('csrftoken');
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });
    }
};


function formserializeJson(formId) {
    var formObj = $('#' + formId);
    var serializeObj = {};
    var array = formObj.serializeArray();
    var str = formObj.serialize();

    $(array).each(function () {
        if (serializeObj[this.name]) {
            if ($.isArray(serializeObj[this.name])) {
                serializeObj[this.name].push(this.value);
            } else {
                serializeObj[this.name] = [serializeObj[this.name], this.value];
            }
        } else {
            serializeObj[this.name] = this.value;
        }
    });
    return serializeObj;
}


function jsonCopyA2B(a, b) {
    for (var key in a) {
        b[key] = a[key];
    }
}

function loadSubPage(url) {
    function callback(data, status, xhr) {
        if (status == "success") {
            $('#main_content').empty();
            $('#main_content').html(data);
        } else {
            $("#main_content").html("错误信息: <br/>" + xhr.status + " " + xhr.statusText)
        }
    }

    // 请求编辑框的页面数据库
    $.get(url, "", callback);
}


/**
 * 弹出显示图片，使用 layui 组件
 * @param obj
 */
function showBoxImage(obj) {
    var layer = layui.layer;
    var url = $(obj).attr('src');
    //iframe层-父子操作
    layer.open({
        type: 2,
        area: ['800px', '500px'],
        title: '图片：',
        fixed: true, //不固定
        maxmin: true,
        content: url
    });
}